import os
from dotenv import load_dotenv

load_dotenv()  # 加载.env文件


class Config:
    # 基础配置
    SECRET_KEY = os.getenv('SECRET_KEY', 'default-secret-key')
    JWT_SECRET_KEY = os.getenv('JWT_SECRET_KEY', SECRET_KEY)

    # 数据库配置
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    MYSQL_HOST = os.environ.get('MYSQL_HOST') or "db"
    MYSQL_USER = os.environ.get('MYSQL_USER') or "user"
    MYSQL_PASSWORD = os.environ.get('MYSQL_PASSWORD') or "example"
    MYSQL_DATABASE = os.environ.get('MYSQL_DATABASE') or "management"
    SQLALCHEMY_DATABASE_URI = f'mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}:3306/{MYSQL_DATABASE}'

    # JWT配置
    JWT_ACCESS_TOKEN_EXPIRES = int(os.getenv('JWT_ACCESS_EXPIRES', 3600))  # 1小时
    JWT_REFRESH_TOKEN_EXPIRES = int(os.getenv('JWT_REFRESH_EXPIRES', 604800))  # 7天

    # 日志配置
    LOG_LEVEL = os.getenv('LOG_LEVEL', 'INFO')

    # 安全配置
    PASSWORD_RESET_EXPIRY = 3600  # 密码重置有效期(秒)

    #用户重置密码有关配置
    FRONTEND_RESET_URL = os.environ.get('FRONTEND_RESET_URL', 'https://your-frontend-app.com/reset_password') # 前端重置页面
    RESET_TOKEN_EXPIRATION = os.environ.get('RESET_TOKEN_EXPIRATION', 3600)
    MAIL_DEFAULT_SENDER = os.environ.get('MAIL_DEFAULT_SENDER', '1551406827@qq.com')  # 默认发件人

    #邮箱配置
    MAIL_SERVER = os.environ.get('MAIL_SERVER')
    MAIL_PORT = int(os.environ.get('MAIL_PORT') or 465)
    MAIL_USE_SSL = os.environ.get('MAIL_USE_SSL')
    MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
    MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
    ADMINS = ['1551406827@qq.com']

    REDIS_URL = os.getenv('REDIS_URL', 'redis://redis:6379/0')

class DevelopmentConfig(Config):
    DEBUG = True


class TestingConfig(Config):
    TESTING = True



class ProductionConfig(Config):
    DEBUG = False

    # 额外安全配置
    SESSION_COOKIE_SECURE = True
    REMEMBER_COOKIE_SECURE = True


config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,
    'default': DevelopmentConfig
}